local sk__weizhen = fk.CreateSkill {

  name = "sk__weizhen",

  tags = { Skill.Compulsory, },

}



sk__weizhen:addEffect(fk.DrawNCards, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__weizhen.name) then
      return (target:getMark("@sk__nizhan") > 1 and data.n > 0) or
          (target == player and table.find(player.room.alive_players, function(p)
            return p:getMark("@sk__nizhan") > 0
          end))
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(sk__weizhen.name)
    if target:getMark("@sk__nizhan") > 1 then
      if target ~= player then
        room:doIndicate(player.id, { target.id })
        room:notifySkillInvoked(player, sk__weizhen.name, "control")
      end
      data.n = data.n - 1
    end
    if target == player then
      room:notifySkillInvoked(player, sk__weizhen.name, "drawcard")
      data.n = data.n + #table.filter(room.alive_players, function(p)
        return p:getMark("@sk__nizhan") > 0
      end)
    end
  end,
})
sk__weizhen:addEffect(fk.DamageCaused, {
  name = "sk__weizhen",
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__weizhen.name) then
      return target and target == player and data.to:getMark("@sk__nizhan") > 2
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    player:broadcastSkillInvoke(sk__weizhen.name)
    room:doIndicate(player.id, { data.to.id })
    room:notifySkillInvoked(player, sk__weizhen.name, "offensive")
    data.damage = data.damage + 1
  end,
})

sk__weizhen:addEffect("invalidity", {
  name = "#sk__weizhen_invalidity",
  --sk__weizhen,
  invalidity_func = function(self, from, skill)
    if from:getMark("@sk__nizhan") > 3 and not skill:hasTag(Skill.Compulsory, true) and
        skill:isPlayerSkill(from) then
      for _, p in ipairs(Fk:currentRoom().alive_players) do
        if p:hasSkill(sk__weizhen.name) then
          return true
        end
      end
    end
  end,
})

return sk__weizhen
